package com.golxen.framework.common.server.game.cmd.session;


import com.golxen.framework.GameProtocol;
import com.golxen.framework.PBPacket.PBPacket;
import com.golxen.framework.common.server.game.cmd.Cmd;
import com.golxen.framework.common.server.game.cmd.Command;
import com.golxen.framework.netty.NettyServices;
import com.golxen.framework.netty.session.base.NettyChannelSession;
import com.golxen.framework.netty.session.base.ServerLinkedGroup;
import com.golxen.framework.netty.session.game.GameServerLinkedSet;
import com.golxen.protojava.LoadProto;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = GameProtocol.S_SERVER_REGISTER, desc = "")
public class GameRegisterCmd implements Command {

	@Override
	public Tuple execute(ChannelHandlerContext ctx, PBPacket packet) throws Exception {
		LoadProto.LoadMsg msg = LoadProto.LoadMsg.parseFrom(packet.toByteArray());
		int serverIndex = msg.getServerIndex();
		int linkIndex = msg.getLinkIndex();

		NettyChannelSession session = new NettyChannelSession(ctx.channel(),  linkIndex);
		ServerLinkedGroup group = GameServerLinkedSet.addAgentLinkedGroup(serverIndex, session,
				NettyServices.LINK_COUNT, linkIndex);
		ctx.channel().attr(NettyServices.LINKED_CLIENT).set(group);

		return new Tuple(null, msg);
	}
}
